package com.zztl.infiniteBand.util.device

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.location.LocationManager
import android.provider.Settings
import com.zztl.infiniteBand.BraceletApplication
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.ui.view.dialog.ExitDialog

/**
 *  @desc
 *  @auth ${user}
 *  @time 2018/10/24  10:40
 */
object DeviceUtil : BluetoothAdapter.LeScanCallback {


    val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
    val DEVICE_NAME = "InfiniteBand"
    //        val DEVICE_NAME = "N88"
    var mFindDeviceCallback: FindDeviceCallback? = null


    fun bluetoothIsEnabel(): Boolean {
        return if (mBluetoothAdapter == null) {
            false
        } else {
            mBluetoothAdapter.isEnabled()
        }
    }

    fun openBluetooth(): Boolean {
        return if (mBluetoothAdapter != null && !mBluetoothAdapter.isEnabled()) {
            mBluetoothAdapter.enable()
        } else false
    }

    fun showturnOnBlutoothAlertView(context: Context) {
        ExitDialog.Builder(context).setMessage(context.getString(R.string.是否开启蓝牙))
                .setClickListener(object : ExitDialog.Builder.ClickListener {
                    override fun cancel() {

                    }

                    override fun comfirm() {
                        openBluetooth()
                    }
                }).create().show()

    }


    fun isGpsEnable(context: Context): Boolean {
        val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        val gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
        val network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
        return gps || network
    }


    fun showturnOnGPSAlertView(context: Context) {
        ExitDialog.Builder(context).setMessage(context.getString(R.string.打开GPS定位))
                .setClickListener(object : ExitDialog.Builder.ClickListener {
                    override fun cancel() {

                    }

                    override fun comfirm() {
                        val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                        context.startActivity(intent)
                    }
                }).create().show()

    }

    fun getConnectedMac(): String {
        val bluetoothManager = BraceletApplication.context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
        val connectedDevices = bluetoothManager.getConnectedDevices(BluetoothGatt.GATT_SERVER)
        if (connectedDevices.size > 0) {
            for (i in connectedDevices.indices) {
                com.zztl.infiniteBand.util.LogUtils.i("FirmwareUpdateActivity", "--" + connectedDevices[i].name)
                connectedDevices[i].name?.let {
                    if (it.contains(DEVICE_NAME)) {
                        return connectedDevices[i].address
                    }
                }
            }
        }
        return ""
    }


    fun getLastConnectMac(): String {
        BraceletApplication.getmDaoSession()?.bindedWatchBeanDao?.loadAll()?.let {
            if (it.size > 0) {
                return it[0].mac
            }
        }
        return ""
    }

    fun scan(findDeviceCallback: FindDeviceCallback) {
        mFindDeviceCallback = findDeviceCallback
        mBluetoothAdapter.startLeScan(this)

    }

    override fun onLeScan(device: BluetoothDevice?, rssi: Int, scanRecord: ByteArray?) {

        mFindDeviceCallback?.apply {
            device?.apply {
                name?.let {
                    findDevice(device)
                }
            }
        }
    }


    fun stopScan() {
        mBluetoothAdapter.stopLeScan(this)
    }

}